package com.wanxi.dao.Impl;

import com.wanxi.dao.ProductDao;
import com.wanxi.model.Product;
import com.wanxi.model.ProductType;
import com.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao {
    @Override
    public List<Product> findAllProduct() {
        List<Product> list=new ArrayList<>();
        String sql="select p.*," +
                "pt.productTypeName from product p,product_type pt where p.productTypeID=pt.id";
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()){
                list.add(Product.builder()
                        .id(query.getInt("id"))
                        .productName(query.getString("productName"))
                        .price(query.getBigDecimal("price"))
                        .imageLocation(query.getString("imageLocation"))
                        .enable(query.getInt("enable"))
                        .createTime(query.getTimestamp("createTime"))
                        .updateTime(query.getTimestamp("updateTime"))
                        .remark(query.getString("remark"))
                        .productType(ProductType.builder().id(query.getInt("productTypeID"))
                                .productTypeName(query.getString("productTypeName"))
                                .build())
                        .build());
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Product findProductById(Product product) {
        String sql="select p.*," +
                "pt.productTypeName from product p,product_type pt where p.id ="+product.getId() ;
        ResultSet query = JDBC.query(sql);
        try{
            while (query.next()){
                return Product.builder()
                        .id(query.getInt("id"))
                        .productName(query.getString("productName"))
                        .price(query.getBigDecimal("price"))
                        .imageLocation(query.getString("imageLocation"))
                        .enable(query.getInt("enable"))
                        .createTime(query.getTimestamp("createTime"))
                        .updateTime(query.getTimestamp("updateTime"))
                        .remark(query.getString("remark"))
                        .productType(ProductType.builder().id(query.getInt("productTypeID"))
                                .productTypeName(query.getString("productTypeName"))
                                .build())
                        .build();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public int updateEnable(Product product) {
        String sql="update product set enable="+product.getEnable()+",updateTime=(select now()) where id="+product.getId();
        return JDBC.update(sql);
    }

    @Override
    public int addProduct(Product product) {
        String sql="insert into product(productName,imageLocation,price,enable,remark,productTypeID,createTime,updateTime) " +
                "values('"+product.getProductName()
                +"','"+product.getImageLocation()
                +"',"+product.getPrice()
                +","+product.getEnable()
                +",'"+product.getRemark()
                +"',"+product.getProductType().getId()
                +",(select now()),(select now()))";
        return JDBC.update(sql);
    }

    @Override
    public int deleteProduct(Product product) {
        String sql="delete from product where id="+product.getId();
        return JDBC.update(sql);
    }

    @Override
    public int updateProduct(Product product) {
        String sql="update product set productName='"+product.getProductName()
                +"',imageLocation='"+product.getImageLocation()
                +"',price="+product.getPrice()
                +",enable="+product.getEnable()
                +",remark='"+product.getRemark()
                +"',productTypeID="+product.getProductType().getId()
                +",updateTime=(select now()) where id="+product.getId();
        return JDBC.update(sql);
    }

    @Override
    public List<Product> findProduct(Product product) {
        List<Product> list=new ArrayList<>();
        String sql="select p.*," +
                "pt.productTypeName from product p,product_type pt where p.productTypeID=pt.id and p.productTypeID="+product.getProductType().getId();
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()){
                list.add(Product.builder()
                        .id(query.getInt("id"))
                        .productName(query.getString("productName"))
                        .price(query.getBigDecimal("price"))
                        .imageLocation(query.getString("imageLocation"))
                        .enable(query.getInt("enable"))
                        .createTime(query.getTimestamp("createTime"))
                        .updateTime(query.getTimestamp("updateTime"))
                        .remark(query.getString("remark"))
                        .productType(ProductType.builder().id(query.getInt("productTypeID"))
                                .productTypeName(query.getString("productTypeName"))
                                .build())
                        .build());
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public List<Product> findProductBySome(Product product) {
        List<Product> list=new ArrayList<>();
        String sql="select p.*," +
                "pt.productTypeName from product p,product_type pt where p.productTypeID=pt.id and p.productName like '%"+product.getProductName()+"%'";
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()){
                list.add(Product.builder()
                        .id(query.getInt("id"))
                        .productName(query.getString("productName"))
                        .price(query.getBigDecimal("price"))
                        .imageLocation(query.getString("imageLocation"))
                        .enable(query.getInt("enable"))
                        .createTime(query.getTimestamp("createTime"))
                        .updateTime(query.getTimestamp("updateTime"))
                        .remark(query.getString("remark"))
                        .productType(ProductType.builder().id(query.getInt("productTypeID"))
                                .productTypeName(query.getString("productTypeName"))
                                .build())
                        .build());
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }
}
